package com.dps.omc.strategy.handler;

import com.dps.omc.model.AgentNode;
import com.dps.omc.model.Server;
import com.dps.omc.model.TemplateHandler;
import com.dps.omc.service.IServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by lip on 2016-02-14.
 */
@Component
public class ServerStep implements IStepHandler{

    @Autowired
    private IServerService serverService;
    @Override
    public void process(AgentNode agentNode, Map<String, String> data, TemplateHandler templateHandler) {

        Server server = serverService.fetch(agentNode.getNodeId());
        if(server!=null)
        {
            data.put("host",server.getHost());
            data.put("user",server.getUser());
            data.put("pass",server.getPass());
            this.processCustomParam(data,server.getCustomParam());
        }
    }

    private void processCustomParam(Map<String, String> data,String customParam)
    {
        if(StringUtils.isNotBlank(customParam)&&customParam.contains(";"))
        {
            String[] arr = customParam.split(";");
            for(int i=0;i<arr.length;i++)
            {
                String keyValue = arr[i];
                if(keyValue.contains("="))
                {
                    String[] kvArr = keyValue.split("=");
                    if(StringUtils.isNotBlank(kvArr[0])&&StringUtils.isNotBlank(kvArr[1]))
                    {
                        data.put(kvArr[0],kvArr[1]);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        ServerStep s = new ServerStep();
        Map<String, String> data = new HashMap<>();
        String customParam = "dbNm=sppos1;dbPort=3306;";
        s.processCustomParam(data,customParam);

        Set<String> set = data.keySet();
        for(String str:set)
        {
            System.out.println(str+"-->"+data.get(str));
        }
    }
}
